--[[
-- Author:      feilong
-- DateTime:    2020-10-10 17:19:33
-- Description: 节点的服务管理
    1更方便的取得节点中各服务的信息，
    2处理节点间的消息
    3开启Gate端口监听消息，client消息由watchdog处理
--]]


local cjson = require("cjson")
local socket = require "skynet.socket"


local M = class("Handler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function M:ctor()
    self:register()
end

--注册本服务里的消息
function M:register()
    g.register('n_web_msg', handler(self, self.onWebMsg)) --从web节点传来的消息
    g.register('n_send_user', handler(self, self.sendUser)) --给用户发消息


    g.register('kick_user', handler(self, self.kickUser)) --踢人
    g.register('robot_msg', handler(self, self.onRobotMsg)) --机器人消息

    g.register('msg_to_match', handler(self, self.onMsgToMatch)) --发给比赛场的消息

end


---------------------------------------------------------
-- CMD
---------------------------------------------------------
function M:onStart()
    g.log:debug("ndgame manger service start!")
    local server_info = g.getServerInfo()
    local protocol = "ws"
    local port = server_info.client_port
    if not port or port == 0 then
        --没有开放端口给客户端
        g.log:debug("server_info.client_port is nil!")
        return
    end
    --开启网络
    g.newService("watchdog.service")
    local id = socket.listen("0.0.0.0", port)
    g.log:finfo("Listen websocket port %s protocol:%s",port, protocol)
    socket.start(id, function(id, addr)
        self:sendWatchdog( "websocket", id, protocol, addr)
    end)


end

--发消息到watchdog
function M:sendWatchdog(cmd, ...)
    g.send(".watchdog.service", cmd, ...)
end

function M:sendUser(uid, ...)
    g.log:info("##111######sendUser:", uid, ...)
    self:sendWatchdog("n_send_user", uid, ...)
end

--踢人
function M:kickUser(aid, reason)
    self:sendWatchdog("kick_user", aid, reason)
end

function M:onWebMsg(msg)
    g.log:debug("manger onWebMsg:", msg)
    local uid = tonumber(msg.mid)
    local data = msg.data
    local ser_cmd = msg.ser_cmd
    local ser_data = msg.ser_data
    if ser_cmd and ser_data then
        --后台发来的消息， 需要服务端处理的
        self:webGM(ser_cmd, ser_data)
    end
    if uid < 0 then return end
    local cmd = "TS_2C_eventBc"
    local msg = {msg=cjson.encode(data)}
    if uid == 0 then
        local mids = msg.mids
        if mids and next(mids) then
            --指定广播的多个用户
            for _, uid in pairs(mids) do
                uid = tonumber(uid)
                self:sendWatchdog("n_send_user", uid, cmd, msg)
            end
            return true
        end
        --广播消息
        self:sendWatchdog("n_send_users", cmd, msg)
        return true
    end
    self:sendWatchdog("n_send_user", uid, cmd, msg)

    return true
end

--后台发来的消息
function M:webGM(cmd, data)
    if cmd == "update_match" then
        --更新比赛场信息
        --g.sendGames(cmd, data)
        g.sendMatch("msg_to_match", cmd, data)
    end
end

--机器人消息
function M:onRobotMsg(...)
    return g.callGames(...)
end

--比赛场消息
function M:onMsgToMatch(...)
    g.log:debug("onMsgToMatch:", ...)
    local res = g.callGames(...)
    if not res then
        g.log:warn("onMsgToMatch res is nil", ...)
        return {code=g.error_code.fail}
    end
    return res
end


return M